package com.huawei.M01.iterator.M17;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 2488. 统计中位数为 K 的子数组
 *
 * @author 111
 */
public class SubArrays {
    public int countSubarrays(int[] nums, int k) {
        List<Integer> cache = new ArrayList<>();
        // 使用双指针
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            cache.add(nums[i]);
            res += getCount(new ArrayList<>(cache), k);
        }
        return res;
    }

    private int getCount(ArrayList<Integer> integers, int nums) {
        int length = integers.size();
        int res = 0;
        for (int i = 0; i < length; i++) {
            List<Integer> tmpList = new ArrayList<>(integers);
            int index = (tmpList.size() + 1) / 2 - 1;
            List<Integer> collect = tmpList.stream().sorted().limit(index).collect(Collectors.toList());
            if (collect.get(collect.size() - 1) == nums) {
                res++;
            }
            integers.remove(0);


        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(new SubArrays().countSubarrays(new int[]{2,3,1}, 3));
        System.out.println(5 / 2);
    }
}
